#!/bin/bash

# GPIO Names Array
gpio_names=(
    "PWRGD_COMPUTE_TRAY1_N"
    "PWRGD_COMPUTE_TRAY2_N"
    "PWRGD_COMPUTE_TRAY3_N"
    "PWRGD_COMPUTE_TRAY4_N"
    "PWRGD_COMPUTE_TRAY5_N"
    "PWRGD_COMPUTE_TRAY6_N"
    "PWRGD_COMPUTE_TRAY7_N"
    "PWRGD_COMPUTE_TRAY8_N"
    "PWRGD_NVS_TRAY1_PWROK_N"
    "PWRGD_NVS_TRAY2_PWROK_N"
    "PWRGD_NVS_TRAY3_PWROK_N"
    "PWRGD_NVS_TRAY4_PWROK_N"
    "PWRGD_NVS_TRAY5_PWROK_N"
    "PWRGD_NVS_TRAY6_PWROK_N"
    "PWRGD_NVS_TRAY7_PWROK_N"
    "PWRGD_NVS_TRAY8_PWROK_N"
    "PWRGD_NVS_TRAY9_PWROK_N"
    "PWRGD_COMPUTE_TRAY9_N"
    "PWRGD_COMPUTE_TRAY10_N"
    "PWRGD_COMPUTE_TRAY11_N"
    "PWRGD_COMPUTE_TRAY12_N"
    "PWRGD_COMPUTE_TRAY13_N"
    "PWRGD_COMPUTE_TRAY14_N"
    "PWRGD_COMPUTE_TRAY15_N"
    "PWRGD_COMPUTE_TRAY16_N"
    "PWRGD_COMPUTE_TRAY17_N"
    "PWRGD_COMPUTE_TRAY18_N"
)


echo "Ventura Init Tray Sgpio Start init"
systemctl stop phosphor-multi-gpio-monitor

for i in "${!gpio_names[@]}"; do

    read -r chip pin <<< "$(gpiofind "${gpio_names[$i]}")"
    gpio_value=$(gpioget "$chip" "$pin")

    # power good is low active pin
    if [ "$gpio_value" == "0" ]; then
        # If power is good, turn on blue LED and turn off amber LED
        busctl set-property "xyz.openbmc_project.LED.GroupManager" "/xyz/openbmc_project/led/groups/leakport$((i+1))_amber" "xyz.openbmc_project.Led.Group" "Asserted" "b" "false"
        busctl set-property "xyz.openbmc_project.LED.GroupManager" "/xyz/openbmc_project/led/groups/leakport$((i+1))_blue" "xyz.openbmc_project.Led.Group" "Asserted" "b" "true"
    fi
done

systemctl start phosphor-multi-gpio-monitor
echo "Ventura Init Tray Sgpio End init"
exit 0
